<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- 
        单向数据流：数据只能从父组件传递给子组件,而子组件不能去修改父组件传进来的值
       -->
      <h2>单向数据流 - {{ count }}</h2>
      <button @click="count++">btn</button>
      <hr />
      <child :count="count" @add="count = 10"></child>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const { createApp } = Vue;

      const Child = {
        // 接受到的props的值是不能被修改，只读的
        props: {
          count: Number,
        },
        data() {
          // 将props的值作为了data的变量的初始值，这个变量就可以被修改了
          return {
            num: this.count,
          };
        },
        template: `
          <div>
            <div>child子组件 - {{ count }}</div>
            <button @click="count++">btn2</button>
            <button @click="$emit('add')">to 10</button>
            <hr />
            <p> num: {{ num }} </p>
            <button @click="num++">num++</button>
          </div>
        `,
      };

      createApp({
        data() {
          return {
            count: 3,
          };
        },
        components: {
          Child,
        },
      }).mount("#app");
    </script>
  </body>
</html>
